package cn.year9;

import org.apache.commons.codec.digest.DigestUtils;


import java.io.File;
import java.util.Scanner;

public class StartMain {
    public static void main(String[] args)
    {
        String user;
        String passowrd;
        Message message = new Message();
        Scanner scanner = new Scanner(System.in);
        message.print(">图灵字节私有数据库服务<",1);
        message.print("           By:year9",1);
        /*判断是否存在数据库*/
        TdbHandle tdb = new TdbHandle();
        if (!tdb.TdbExist())
        {

            message.send(MessageType.Error,"没有找到数据库文件,是否新建? [Y/Yes] [N/No] : ");
            if (scanner.next().equalsIgnoreCase("y"))
            {
                showCreateTdb();
            }else{
                message.send(MessageType.Error,"缺乏数据库无法继续运行,程序退出");
                return;
            }

        }else {
            try {
                int flag = tdb.contactTdbtest();
           if ( flag != 500)
           {
               /*开始校验用户和密码*/
               message.print(">请使用用户与授权码访问<", 1);
               message.setPrefix("验证");
               message.send(MessageType.Info, "用户名:");
               user = scanner.next();
               message.send(MessageType.Info, "密码:");
               passowrd = scanner.next();
               /*调用账号密码验证器*/
               if (!tdb.UserValidate(user, DigestUtils.md5Hex(passowrd))) {
                   message.send(MessageType.Warning, "用户名或密码错误!");
                   return;
               }

               /*调入界面类*/

               message.send(MessageType.Info,"验证成功,正在转入数据管理系统..\n");
               TableShow tableShow = new TableShow();
               tableShow.showTable();


           }else if( flag == 500)
           {
               message.send(MessageType.Error,"服务器数据库标识没有响应");
               return;
           }else {
               message.send(MessageType.Warning,"遇到意料之外的错误..");
           }

            }catch (Exception e)
            {
                e.printStackTrace();
                message.send(MessageType.Error,"数据库结构体异常\n");
                message.send(MessageType.Info,"是否删除损坏数据库创建新数据库? (Y/N):");
                if (scanner.next().equalsIgnoreCase("y"))
                {
                    File file = new File("Data.tdb");
                    if (file.exists())
                    {
                        file.delete();
                    }
                    showCreateTdb();
                }
            }
        }
    }

    public static void showCreateTdb()
    {
        Message message = new Message();
        Scanner scanner = new Scanner(System.in);
        TdbHandle tdb = new TdbHandle();
        message.send(MessageType.Info,"将创建的用户名:");
        String puser = scanner.next();
        message.send(MessageType.Info,"将创建的密码:");
        String ppassword = scanner.next();
        message.send(MessageType.Info,"将创建的数据库加密密码:");
        String TdbPassword = scanner.next();

        String Enppassword = DigestUtils.md5Hex(ppassword);
        String EnTdbPassword = DigestUtils.md5Hex(TdbPassword);

        if (tdb.createTdb(puser,Enppassword,EnTdbPassword))
        {
            message.send(MessageType.Info,"数据库初始化完毕! 重启项目以读取！");
            return;
        }
    }
}
